package com.example.database;

import java.util.ArrayList;
import java.util.List;

import com.example.item.ItemList;
import com.example.item.ItemSMS;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	static final String dbName = "smssecurityDB";
	static final String tblPhone = "tblPhone";
	static final String sodienthoai = "sodienthoai";
	static final String hoten = "hoten";
	static final String tinchuadoc = "tinchuadoc";

	static final String tblSms = "tblSms";
	static final String id = "id";
	static final String sodt = "sodt";
	static final String noidung = "noidung";
	static final String ngaygui = "ngaygui";
	static final String guinhan = "guinhan";
	static final int dbversion = 1;

	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, dbName, factory, dbversion);
		// TODO Auto-generated constructor stub
	}

	public DatabaseHelper(Context context) {
		super(context, dbName, null, dbversion);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE tblPhone(sodienthoai VARCHAR(15) PRIMARY KEY,hoten TEXT,tinchuadoc INTEGER NOT NULL DEFAULT '0')");
		db.execSQL("CREATE TABLE tblSms(id INTEGER PRIMARY KEY AUTOINCREMENT,sodt VARCHAR(15) NOT NULL,FOREIGN KEY(sodt) REFERENCES tblPhone(sodienthoai, noidung VARCHAR(320), ngaygui DATETIME(), guinhan INTEGER))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	public void addNewContact(ItemList contact) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues value = new ContentValues();

		value.put(sodienthoai, contact.getSodienthoai());
		value.put(hoten, contact.getHoten());
		value.put(tinchuadoc, contact.getTinchuadoc());
		db.insert(tblPhone, null, value);
		db.close();
	}

	public boolean checkPhone(String phone) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(tblPhone, new String[] { sodienthoai, hoten,
				tinchuadoc }, sodienthoai + "=?", new String[] { phone }, null,
				null, null);
		if (cursor.getCount() > 0) {
			return false;
		}
		db.close();
		return true;

	}

	public ArrayList<ItemList> getAllContact() {
		ArrayList<ItemList> list = new ArrayList<ItemList>();

		SQLiteDatabase db = this.getReadableDatabase();
		String query = "SELECT * FROM tblPhone";
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			Log.i("Helper", "afggasdg");
			do {
				ItemList contact = new ItemList();
				contact.setSodienthoai(cursor.getString(0));
				contact.setHoten(cursor.getString(1));
				// ItemList contact = new
				// ItemList(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)));
				list.add(contact);
			} while (cursor.moveToNext());
			db.close();
			return list;
		}
		db.close();
		return null;
	}

	public ArrayList<ItemSMS> getMessageByPhone(String sodienthoai) {
		ArrayList<ItemSMS> list = new ArrayList<ItemSMS>();
		ItemSMS message;
		
		SQLiteDatabase db = this.getReadableDatabase();
		String query = "SELECT * FROM tblSms WHERE sodt=\"+sodienthoai\"";
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			Log.i("Helper", "afggasdg");
			do {
				message = new ItemSMS();
				message.setMessaging(cursor.getString(2));
				list.add(message);
			} while (cursor.moveToNext());
			db.close();
			return list;
		}
		db.close();
		return null;
	}

	public void deleteContact(String phone) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(tblPhone, sodienthoai + "=?", new String[] { phone });
		db.close();
	}
}
